<?php
namespace app\api\controller\book;
use app\Response;
use QL\QueryList;
use \Curl\Curl;
use app\common;
use think\Db;

class Biquge {
	const WEB = 'http://www.biquge5200.com/';

	const SEARCHURL = 'http://www.biquge5200.com/modules/article/search.php?searchkey=';

	public  function index($key='', $url='', $step=array(), $book_link_id='', $chapter_link_id=''){
		set_time_limit(0);

		if($url){
			$chapter_down = self::chapterLink($url, $book_link_id, 0);
		}
		//第一步，搜索小说
		if($key && (empty($step) || in_array(1, $step))){
			$book_total = self::search($key);
			$result[] = '共录入'.$book_total.'本小说';
		}
		//第四步，只搜索
		if($key && (empty($step) || in_array(4, $step))){
			$book_total = self::justSearch($key);
			$result[] = '共录入'.$book_total.'本小说';
		}

		//第二步，获取小说章节链接信息
		if(empty($step) || in_array(2, $step)){
			$chapter_link_total = 0;
			if(!$book_link_id){
				//获取所有的小说链接
				$book_link_info = Db::table('story_book_link')->select();
				if($book_link_info){
					foreach ($book_link_info as $key => $val) {
						$link 		= isset($val['link'])?$val['link']:'';
						$book_link_id 	= isset($val['book_link_id'])?$val['book_link_id']:'';
						$txt_id 		= isset($val['txt_id'])?$val['txt_id']:'';
						$chapter_link_total += self::chapterLink($link, $book_link_id, $txt_id);
					}
				}
			}else{
				$book_link_info = Db::table('story_book_link')->where('book_link_id', $book_link_id)->find();
				$link 		= isset($book_link_info['link'])?$book_link_info['link']:'';
				$book_link_id 	= isset($book_link_info['book_link_id'])?$book_link_info['book_link_id']:'';
				$txt_id 		= isset($book_link_info['txt_id'])?$book_link_info['txt_id']:'';
				$chapter_link_total = self::chapterLink($link, $book_link_id, $txt_id);
			}
			$result[] = '共录入'.$chapter_link_total.'个章节链接';
		}

		//第三步，获取章节内容
		if(empty($step) || in_array(3, $step)){
			$content_total = 0;
			if(!$book_link_id){
				//获取所有的小说的章节链接
				while ($chapter_link_info = Db::table('story_chapter_link')->where('is_save',0)->limit(10)->select()) {
					foreach ($chapter_link_info as $key => $val) {
						$chapter_link_id= isset($val['chapter_link_id'])?$val['chapter_link_id']:'';
						$link 			= isset($val['link'])?$val['link']:'';
						$book_link_id 	= isset($val['book_link_id'])?$val['book_link_id']:'';
						$txt_id 		= isset($val['txt_id'])?$val['txt_id']:'';
						$chapter_id 	= isset($val['chapter_id'])?$val['chapter_id']:'';
						$chapter_name 	= isset($val['chapter_name'])?$val['chapter_name']:'';
						$content  		= self::getContent($link, $txt_id, $chapter_id, $chapter_name);
						if($content){
							$update_data = array('is_save'=>1);
							$update_key  = array('chapter_link_id'=>$chapter_link_id);
							$update = common::updateTbl('story_chapter_link', $update_data, $update_key);
							$content_total++;
						}
					}
				}			
			}else if($book_link_id){
				while ($chapter_link_info = Db::table('story_chapter_link')->where('book_link_id', $book_link_id)->where('is_save',0)->limit(10)->select()) {
					foreach ($chapter_link_info as $key => $val) {
						$chapter_link_id= isset($val['chapter_link_id'])?$val['chapter_link_id']:'';
						$link 			= isset($val['link'])?$val['link']:'';
						$book_link_id 	= isset($val['book_link_id'])?$val['book_link_id']:'';
						$txt_id 		= isset($val['txt_id'])?$val['txt_id']:'';
						$chapter_id 	= isset($val['chapter_id'])?$val['chapter_id']:'';
						$chapter_name 	= isset($val['chapter_name'])?$val['chapter_name']:'';
						$content  		= self::getContent($link, $txt_id, $chapter_id, $chapter_name);
						
						if($content){
							$update_data = array('is_save'=>1);
							$update_key  = array('chapter_link_id'=>$chapter_link_id);
							$update = common::updateTbl('story_chapter_link', $update_data, $update_key);
							$content_total++;
						}
					}
				}
			}else{
				$chapter_link_info = Db::table('story_chapter_link')->where('book_link_id', $book_link_id)->find();
				$chapter_link_id= isset($chapter_link_info['chapter_link_id'])?$chapter_link_info['chapter_link_id']:'';
				$link 			= isset($chapter_link_info['link'])?$chapter_link_info['link']:'';
				$book_link_id 	= isset($chapter_link_info['book_link_id'])?$chapter_link_info['book_link_id']:'';
				$txt_id 		= isset($chapter_link_info['txt_id'])?$chapter_link_info['txt_id']:'';
				$chapter_id 	= isset($chapter_link_info['chapter_id'])?$chapter_link_info['chapter_id']:'';
				$chapter_name 	= isset($chapter_link_info['chapter_name'])?$chapter_link_info['chapter_name']:'';
				$content 		= self::getContent($link, $txt_id, $chapter_id, $chapter_name);
				if($content){
					$update_data = array('is_save'=>1);
					$update_key  = array('chapter_link_id'=>$chapter_link_id);
					$update = common::updateTbl('story_chapter_link', $update_data, $update_key);
					$content_total++;
				}
			}
			$result[] = '共录入'.$content_total.'个章节内容';
		}

		return $result;
	}
	/**
	 * [getContent 获取章节内容]
	 * @param  string $chapter_link [章节连接]
	 * @param  string $txt_id       [书籍ID]
	 * @param  string $chapter_id   [章节ID]
	 * @param  string $chapter_name [章节名称]
	 * @return [type]               [description]
	 */
	public static function getContent($chapter_link='', $txt_id='', $chapter_id='', $chapter_name=''){
		$result = false;
		if($chapter_link){
			$rule1 = array(
			    	'txt' => array('#content','html')     //获取小说内容
			    	);

			$ql = QueryList::get($chapter_link);
			$html = $ql->getHtml();
			
			if($html){
				//获取网页编码
				$charset = self::getCharset($html);				

				$data = QueryList::html($html)->rules($rule1)->query()->getData()->all();

				if($data){
					foreach ($data as $key => $val) {
						$txt = isset($val['txt'])?$val['txt']:'';
						if($charset && $charset != 'utf-8'){
							$txt = common::encoding($txt);						
						}

						$time = date('Y-m-d H:i:s');
						$chapter_data = array(
							'txt_id' 		=> $txt_id,
							'chapter_id' 	=> $chapter_id,
							'chapter_name' => $chapter_name,
							'content' 		=> $txt,
							'create_time' 	=> $time,
							'update_time'   => $time
							);
						$chapter_key = array('txt_id','chapter_id');
						$add_chapter = common::addTbl('story_chapter',$chapter_data,$chapter_key);
						if($add_chapter){
							$result = true;
						}
					}
				}
			}
		}
		return $result;
	}

	/**
	 * [chapterLink 获取章节链接]
	 * @param  string $book_link    [小说链接]
	 * @param  string $book_link_id [小说链接ID]
	 * @param  string $txt_id       [小说ID]
	 * @return [type]               [description]
	 */
	public static function chapterLink($book_link='', $book_link_id='', $txt_id=''){
		$total = 0;
		if($book_link){
			$rule1 = array(
			    	'link' => array('dd>a','href'),     //获取小说名和链接
			    	'title' => array('dd>a','text')
			    	);
			$ql = QueryList::get($book_link);
			$html = $ql->getHtml();

			if($html){
				//获取网页编码
				$charset = self::getCharset($html);

				$data = QueryList::html($html)->rules($rule1)->query()->getData()->all();
				if($data){
					$chapter_id = 1;
					foreach ($data as $key => $val) {
						//去掉前面9章，笔趣阁页面的前面9章是最新的9个章节，排序应该靠后
						if($key>8){
							$link = isset($val['link'])?$val['link']:'';
							$title = isset($val['title'])?$val['title']:'';
							if($charset && $charset != 'utf-8'){
								$title = common::encoding($title);
							}
							if($link){
								$time = date('Y-m-d H:i:s');
								$chapter_link_data = array(
									'book_link_id' 	=> $book_link_id,
									'txt_id' 		=> $txt_id,
									'chapter_id' 	=> $chapter_id,
									'chapter_name' => $title,
									'link' 		=> $link,
									'web' 		=> $book_link,
									'create_time' 	=> $time,
									'update_time'   => $time
									);
								$chapter_link_key = array('chapter_name','link');
								$add_chapter_link = common::addTbl('story_chapter_link',$chapter_link_data,$chapter_link_key);
								if($add_chapter_link){
									$total++;
								}
							}
							$chapter_id++;
						}					
					}
				}
			}
		}
		return $total;
	}
	/**
	 * [search 笔趣阁搜索小说]
	 * @param  [string] $key [关键字]
	 * @return [type]      [description]
	 */
	public static function justSearch($key){
		$web = self::WEB;

		$search_url = self::SEARCHURL.$key;			

		$rule1 = array(
		    	'link' => array('.odd>a','href'),     //获取小说名和链接
		    	'book' => array('.odd>a','text')
		    	);

		$rule2 = array(
			'info' => array('#info>p', 'text')    //获取小说的作者
			);

		//$html = common::getWeb($search_url);

		$ql = QueryList::get($search_url);
		$html = $ql->getHtml();

		if(!$html){
			Response::show(500,"请求{$title_url}失败");
			exit;
		}

		//获取网页编码
		$charset = self::getCharset($html);

		$data = QueryList::html($html)->rules($rule1)->query()->getData()->all();

		$total = 0;
		$story_data = array();
		if($data){
			foreach ($data as $m => $o) {
				$book_info = array();

				$link 	= isset($o['link'])?$o['link']:'';
				$link 	= stristr($link, $web)?$link:($web .'/'. ltrim($link, '/'));

				$book 	= isset($o['book'])?$o['book']:'';
				if($charset && $charset != 'utf-8'){
					$book = common::encoding($book);
				}

				$author = '';

				if($link){
					$book_link = QueryList::get($link);
					$book_html = $book_link->getHtml();
					//获取网页编码
					$web_charset = self::getCharset($book_html);

					$author_info = QueryList::html($book_html)->rules($rule2)->query()->getData()->all();
					if($author_info){
						$info = isset($author_info[0]['info'])?$author_info[0]['info']:'';
						
						if($info){
							if($web_charset && $web_charset != 'utf-8'){
								$info = common::encoding($info);
							}
							$info_Arr = array();
							//英文冒号
							if(stristr($info, ':')){
								$info_Arr = explode(':', $info);
							}else if(stristr($info, '：')){
								//中文冒号
								$info_Arr = explode('：', $info);
							} 
							$author = isset($info_Arr[1])?trim($info_Arr[1]):'';
						}
					}
				}
					if($author){
						$time = date('Y-m-d H:i:s');
						//插入小说数据表
						$story_data[] = array(
								'name' 	 => $book,
								'author' => $author,
								'link' 	 => $mulu_url,
								'web' 	 => $web
								);
						$story_key = array('name', 'author');
						$search = common::addTbl('web_search', $story_data, $story_key);
						if($search){
							$total++;
						}								
					}
				}
			}
		}
		return $total;
	}
	/**
	 * [search 笔趣阁搜索小说]
	 * @param  [string] $key [关键字]
	 * @return [type]      [description]
	 */
	public static function search($key){
		$web = self::WEB;

		$search_url = self::SEARCHURL.$key;			

		$rule1 = array(
		    	'link' => array('.odd>a','href'),     //获取小说名和链接
		    	'book' => array('.odd>a','text')
		    	);

		$rule2 = array(
			'info' => array('#info>p', 'text')    //获取小说的作者
			);

		//$html = common::getWeb($search_url);

		$ql = QueryList::get($search_url);
		$html = $ql->getHtml();

		if(!$html){
			Response::show(500,"请求{$title_url}失败");
			exit;
		}

		//获取网页编码
		$charset = self::getCharset($html);

		$data = QueryList::html($html)->rules($rule1)->query()->getData()->all();

		$total = 0;
		
		if($data){
			foreach ($data as $m => $o) {
				$book_info = array();

				$link 	= isset($o['link'])?$o['link']:'';
				$link 	= stristr($link, $web)?$link:($web .'/'. ltrim($link, '/'));

				$book 	= isset($o['book'])?$o['book']:'';
				if($charset && $charset != 'utf-8'){
					$book = common::encoding($book);
				}

				$author = '';

				if($link){
					$book_link = QueryList::get($link);
					$book_html = $book_link->getHtml();
					//获取网页编码
					$web_charset = self::getCharset($book_html);

					$author_info = QueryList::html($book_html)->rules($rule2)->query()->getData()->all();
					if($author_info){
						$info = isset($author_info[0]['info'])?$author_info[0]['info']:'';
						
						if($info){
							if($web_charset && $web_charset != 'utf-8'){
								$info = common::encoding($info);
							}
							$info_Arr = array();
							//英文冒号
							if(stristr($info, ':')){
								$info_Arr = explode(':', $info);
							}else if(stristr($info, '：')){
								//中文冒号
								$info_Arr = explode('：', $info);
							} 
							$author = isset($info_Arr[1])?trim($info_Arr[1]):'';
						}
					}
				}
				if($author){
					$time = date('Y-m-d H:i:s');
					//插入小说数据表
					$story_data = array(
						'name' => $book,
						'author' => $author,
						'create_time' => $time,
						'update_time' => $time
						);
					$story_key = array('name', 'author');
					$txt_id = common::addTbl('story', $story_data, $story_key);

					if($txt_id){
						//插入小说来源信息表
						$book_info = array(
							'txt_id' 		=> $txt_id,
							'link' 		=> $link,
							'web' 		=> $web,
							'create_time' 	=> $time,
							'update_time' 	=> $time
						);
						$book_key = array('txt_id');
						$book_link_id = common::addTbl('story_book_link', $book_info, $book_key);
						if($book_link_id){
							$total++;
						}
					}
				}
			}
		}
		return $total;
	}
	/**
	 * [getCharset 获取网页编码]
	 * @param  [type] $html [description]
	 * @return [type]       [description]
	 */
	public static function getCharset($html){
		$charset = '';

		if($html){
			$rules = array(
				'meta' => array('meta','content')           //获取网页头信息，主要为了获取网页编码
				);

			// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
			$web_header = QueryList::html($html)->rules($rules)->query()->getData();
			
			if($web_header){
				foreach ($web_header as $key => $val) {
					$meta = isset($val['meta'])?$val['meta']:'';
					if(stristr($meta, 'charset')){
						$meta = strtolower($meta);                    //统一转化为小写
						$meta_Arr = explode('=', $meta);
						$charset = isset($meta_Arr[1])?$meta_Arr[1]:'';
					}
				}
			}
		}
		return $charset;
	}

}